package com.jwell.panrank.ui.adapter

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.jwell.panrank.R
import com.jwell.panrank.entity.ElectronicePoundBean
import com.jwell.panrank.tools.StringUtils
import com.jwell.panrank.ui.activity.ElectronicPoundInfoActivity
import kotlinx.android.synthetic.main.item_electronic_layout.view.*

/**
 * Created by 76577 on 2018/4/8.
 * 电子磅单适配器
 */
class ElectronicAdapter constructor(list: List<ElectronicePoundBean>) : RecyclerView.Adapter<ElectronicAdapter.ViewHolder>() {
    private var list = list

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_electronic_layout, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.itemView.setOnClickListener {
            var intent = Intent(holder.itemView.context, ElectronicPoundInfoActivity::class.java)
            var bundle = Bundle()
            bundle.putSerializable("itemData", list[position])
            intent.putExtras(bundle)
            holder.itemView.context.startActivity(intent)
        }
        with(holder.itemView) {
            when (list[position].type) {
                1 -> {
                    text_weight_state.text = "重"
                    text_weight_state.setBackgroundResource(R.drawable.bg_orange10)
                    text_weight_state.setTextColor(context.resources.getColor(R.color.cff9429))
                }
                2 -> {
                    text_weight_state.text = "空"
                    text_weight_state.setBackgroundResource(R.drawable.bg_red10)
                    text_weight_state.setTextColor(context.resources.getColor(R.color.c3000ff))
                }
                else -> text_weight_state.text = ""
            }
            text_measurementType.text = list[position].measurementTypeName ?: ""
            text_time.text = StringUtils.getInstance()
                    .setModal(StringUtils.TIME_TYPE_3)
                    .regularization(list[position].carMeasureTime, "T", " ")
            //外购大宗原燃料显示供应商，其他显示发货单位
            when (list[position].measurementType) {
                3 -> {
                    text_tag_1.text = "供"
                    text_tag_1.setBackgroundResource(R.drawable.bg_text_green)
                    text_tag_1.setTextColor(resources.getColor(R.color.green))
                    text_startCom.text = list[position].supplierName ?: ""
                }
                else -> {
                    text_tag_1.text = "发"
                    text_tag_1.setBackgroundResource(R.drawable.bg_text_red)
                    text_tag_1.setTextColor(resources.getColor(R.color.red))
                    text_startCom.text = list[position].forwardingUnit ?: ""
                }
            }
            text_receiptCom.text = list[position].receivingUnit ?: ""
            var str = StringBuffer()
            str.append(list[position].driverName ?: "")
            if ("" != str.toString() && list[position].carNum != null) {
                str.append("/")
            }
            str.append(list[position].carNum ?: "")
            text_driverInfo.text = str
            text_goodsName.text = list[position].materialName ?: ""
            if (list[position].netWt > 0) {
                text_weight_tag.visibility = View.VISIBLE
                text_weight.text = list[position].netWt.toString() + "吨"
                text_weight.visibility = View.VISIBLE
            } else {
                text_weight_tag.visibility = View.GONE
                text_weight.visibility = View.GONE
                text_weight.text = ""
            }
            text_code.text = list[position].forBillCode ?: ""
        }
    }
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}